package cn.huang.controller;

import cn.huang.constant.MessageConstant;
import cn.huang.entity.PageResult;
import cn.huang.entity.QueryPageBean;
import cn.huang.entity.Result;
import cn.huang.pojo.Role;
import cn.huang.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 角色管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    //添加功能
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.add(role,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            //角色新增失败
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        //角色新增成功
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
    }

    //删除功能
    @RequestMapping("delete_Role_Id")
    public Result delete_Role_Id(Integer roleId){
        try {
            roleService.dele_Role_Id(roleId);
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
    }

    //根据ID查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
           Role role = roleService.findById(id);
           //角色查询成功
           return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            //角色查询失败
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据角色 id 查询 角色项中所包含的多个权限项的id
    @RequestMapping("findPermissionIdByRoleId")
    public Result findPermissionIdByRoleId(Integer id){
        try {
           List<Integer> permissionIds = roleService.findPermissionIdByRoleId(id);
           //角色查询成功
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            //角色查询失败
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //编辑角色项
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role , Integer[] permissionIds){
        try {
            roleService.edit(role,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            //编辑角色项失败
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
        //编辑角色成功
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
           List<Role> list = roleService.findAll();
           //查询成功
           return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            //查询失败
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
